<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of report
 *
 * @author Константин Охотник
 */

class Orders extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->issetSession('Orders');
        $this->load->model('OrdersModel');
        $this->addcat('gl_categories', 'params');
        $this->addcat('tb_fabricator', 'fabricator');
    }
    
    private function addcat($tb, $params)
    {
        $params_array = $this->OrdersModel->AddParams($tb);
        $this->crud->assign($params ,$params_array);
    }
    
    public function params()
    {
        $data = $this->input->post();
        $params_array = $this->OrdersModel->AddParams($data['tb'],$data['id_category'],$data['id_fabricator']);
        ob_end_clean();
        header('Content-Type: application/json');   
        echo(json_encode($params_array));
    }
    
    public function index($p=false)
    {
        $content_array = $this->OrdersModel->getOrders(base_url()."orders/index/",PER_PAGE_CRM);
        $pages = $this->pagination->create_links();
        $this->crud->assign('orders' ,$content_array)
            ->assign('pages', $pages)->display('orders.tpl');
    }
    
    public function add()
    {
       die($this->OrdersModel->Add($this->input->post()));
    }
    
    public function addcurrent()
    {
        die($this->OrdersModel->AddCurrent($this->input->post()));
    }
    
    
    public function delete($id)
    {
        $this->OrdersModel->delete_post('tb_order', 'id', $id);
        redirect(site_url("orders/"), 'refresh');
    }
   
    public function find()
    {
        $p = $this->input->get();
        if(trim($p['status']) ==""&&trim($p['name'])==""&&trim($p['phone'])=="")
        {
            redirect(site_url("orders/"), 'refresh');
        }
        $content_array= $this->OrdersModel->Filter(base_url()."orders/find/",$this->input->get(),PER_PAGE_CRM);
        $pages = $this->pagination->create_links();

        if(isset($content_array['error']))
        {
            $this->crud->assign('error' ,$content_array)->display('orders.tpl'); 
        }
        else
        {
            $this->crud->assign('orders' ,$content_array)
                ->assign('pages', $pages)->display('orders.tpl');
        }
        
    }
}